/**
 * File: cs3343-Satir-Therapy/src/program/GamePage.java
 *  
 * Responsible author: Lai Hing Hung, Dennis					Revision no: 1.0.0
 * Checked in at date: 2012/04/06 16:52
 * Log: initialize the main flow of game program
 * 		remove displayMainMenu()
 * 		implement selection menu on startup like number of players, input of player name
 * 		use isInteger() to verify whether the input is integer
 * 		use vector to hold players, remove option 2 - skip the turn to the next player 
 *      add save game function and load game function
 *      use BoardProcessor class to draw the board
 *      use DiceProcessor class to generate the random number
 *      define updatePage() method
 *      add array variable to hold the player roles instead of primitive variable
 *      restrict user to choose 1 or 2 in the game menu
 *      define displayCardInfo() method
 *      change updatePage() signature from "boolean updatePage()" to "void updatePage()"
 * 		add checkGridType() and halt()
 * 		add pauseProgram() to allow user to see the screen before refreshing the screen
 * 		add do-while loop to contain the option until user rolls the dice
 * 		trigger corresponding incident when player step onto tool card grid, event card grid and say/action card grid
 * 		rename checkGridType() into triggerGridIncident()
 * 		extract the function of saving game into saveGame() method
 * 		extract the function of analysis into analyzeFamilyRelationship() method
 * 		force user input at least one role as a child like son or daughter
 * 		add checkPlayerMagicTool(), displayCardInfo(), displayEventQuestion(), displayOtherPlayer(), displaySAQuestion()
 * 		add displayYN(), isPlayerMagicToolAvailable(), isWinnerExisted(), selectPlayerMagicTool()
 * 		add generateReport() for printing functionality
 * 		debug the input of user name and user role
 * 		extract the function of loading game into loadGame() method
 * 		
 */
package program;

import java.io.*;
import java.text.AttributedString;
import java.util.*;
import java.util.regex.*;

public class GamePage {
	
	int numPlayers;
	String numberOfPlayers;
	String roundNo = "";
	
	BoardProcessor boardProcessor = new BoardProcessor();
	CardProcessor cardProcessor = new CardProcessor();
	
	Player player;
	Player p1;
	Player p2;
	Player p3;
	Player p4;
	Player p5;
	Player p6;
	Vector<Record> records = new Vector<Record>();	
	Vector<Player> players = new Vector<Player>();
//	Player player;
	/**
 * Instantiates a new game page.
 */
	public GamePage(){
			
			String selection="";
			
			Scanner scanner = new Scanner(System.in);
			
			do{
				System.out.println("Welcome to Satir Therapy!");
				System.out.println("1. Start a new game!");
				System.out.println("2. Load a game!");
				System.out.println("3. quit");
				System.out.print("Enter your selection (1, 2 or 3): ");
				selection = scanner.nextLine();
				if(isInteger(selection)){
					if(selection.equals("1")){					
						break;
					}
					else if(selection.equals("2")){
						if(loadGame()==true){
							break;
						}
					}
					else if(selection.equals("3")){
						System.exit(0);
					}
					else {
						System.out.println("You should input 1, 2 or 3. Try again!");
					}
				}
			}while(true);
			System.out.println();			
//			System.out.println("You select " + selection);
			if(selection.equals("1")){
			
				do{			
					System.out.print("Enter the number of players (2 - 6): ");
					numberOfPlayers = scanner.nextLine();
					if(isInteger(numberOfPlayers)){
						if(numberOfPlayers.equals("2") || numberOfPlayers.equals("3") 
							|| numberOfPlayers.equals("4") || numberOfPlayers.equals("5") || numberOfPlayers.equals("6"))
							break;
					}
					else{
						System.out.println("Please input integer for the number of players in range of 2 - 6. Try again!");
					}
				}while(true);
				System.out.println();
				
				// player id
				int id = 1;
				String playerName[] = new String[6];
				String playerRole[] = new String[6];
				
				playerName[0]="";
				playerName[1]="";
				playerName[2]="";
				playerName[3]="";
				playerName[4]="";
				playerName[5]="";
				
	//			String player1Role="";
	//			String player2Role="";
	//			String player3Role="";
	//			String player4Role="";
	//			String player5Role="";
	//			String player6Role="";
				
		//		int nPlayers = Integer.parseInt(numberOfPlayers);
				numPlayers = Integer.parseInt(numberOfPlayers);
				int num = numPlayers;
				
				String role[] = {"Father", "Mother", "Son", "Daughter", "GrandPa", "GrandMa"};
				
				int index=0;	// looping index
				
				boolean atLeastOneChild = false;
				
				while(num>0){
//				System.out.println("NOW IS NUM " +num);	
					while(true){
						System.out.print("Please Input player " + id + "\'s name: ");			
						playerName[index]=scanner.nextLine();
						if(playerName[index].isEmpty()){
							System.out.println("Player cannot be empty! Please input your name!");
						}
						else{
							if(playerName[index].length()>8){
								System.out.println("Player name should not be larger than 8 characters. Try again!");
							}
							else{
								break;
							}
						}
					}
					
//					boolean atLeastOneChild = false;
					
					while(true){
						System.out.print(playerName[index] + ", Which role do you want to play, ");
						boolean overlap = false;
						for(int i=0; i<6; i++){
							for(int j=0; j<=index; j++){
								if(role[i].equals(playerRole[j])){
									overlap = true;
									//System.out.print(role[i] + "?");
								}
								if(j==index){
									if(overlap==false)
										System.out.print(role[i] + "? ");							
								}
							}
							overlap = false;
						}
						
						playerRole[index] = scanner.nextLine();
						if(playerRole[index].equalsIgnoreCase(role[0]) || playerRole[index].equalsIgnoreCase(role[1]) 
								|| playerRole[index].equalsIgnoreCase(role[2]) || playerRole[index].equalsIgnoreCase(role[3]) 
								|| playerRole[index].equalsIgnoreCase(role[4]) 
								|| playerRole[index].equalsIgnoreCase(role[5])){
							
							if(playerRole[index].equalsIgnoreCase("son") || playerRole[index].equalsIgnoreCase("daughter")){
								atLeastOneChild = true;
							}
							
							if(index>0){
								overlap = false;
								for(int k=0; k<index; k++){
									if(playerRole[index].equals(playerRole[k])){
										System.out.println("The role you chosen overlaps other player(s). Please try another one!");
										overlap = true;
									}
								}
								
								if(overlap==false){
									
									if(num==1){
										if(atLeastOneChild==true){
											num--;
											break;
										}
										else{
											System.out.println("The game should involve at least one child like son or daughter. Please input the role again!");
										}
									}
									else{
//										System.out.println("NOW IS " + num);
										num--;
										break;
									}																		
								}
							}
							else{
								num--;
								break;
							}
						}
						else{
							System.out.println("Your input is invalid. Please input one of them: Father, Mother, Son, Daughter, GrandPa, GrandMa!");
						}
					}
	
					index++;					
					id++;
				}
				System.out.println();
				
				p1 = new Player("1", playerName[0], playerRole[0], 1, 5);
				p2 = new Player("2", playerName[1], playerRole[1], 1, 5);
				p3 = new Player("3", playerName[2], playerRole[2], 1, 5);
				p4 = new Player("4", playerName[3], playerRole[3], 1, 5);
				p5 = new Player("5", playerName[4], playerRole[4], 1, 5);
				p6 = new Player("6", playerName[5], playerRole[5], 1, 5);
				
				switch(numPlayers){
					case 2: players.add(p1);players.add(p2);break;
					case 3: players.add(p1);players.add(p2);players.add(p3);break;			
					case 4: players.add(p1);players.add(p2);players.add(p3);players.add(p4);break;
					case 5: players.add(p1);players.add(p2);players.add(p3);players.add(p4);players.add(p5);break;	
					case 6: players.add(p1);players.add(p2);players.add(p3);players.add(p4);players.add(p5);players.add(p6);break;
				}
					
			}
			
			if(selection.equals("2")){
				int numberOfRecords = records.size();
				
				for(int i=0, j=1; i<numberOfRecords; i++, j++){
					
					System.out.println(i + " " + records.get(i).getDate());
				}
				
				String recordNo;
				do{			
					System.out.print("Enter the record number to load the game: ");
					recordNo = scanner.nextLine();
					if(isInteger(recordNo)){
						if(Integer.parseInt(recordNo)<0 || Integer.parseInt(recordNo)>numberOfRecords){
							System.out.println("The record number you input is out of range! Try again!");
						}
						else{
							break;
						}
					}
					else{
						System.out.println("Please input integer for the record number to load the game. Try again!");
					}
				}while(true);
				
				int index = Integer.parseInt(recordNo);
				numberOfPlayers = records.get(index).getNumberOfPlayers();

				numPlayers = Integer.parseInt(numberOfPlayers);

				p1 = records.get(index).getPlayers().get(0);
				p2 = records.get(index).getPlayers().get(1);
				p3 = new Player("3", "", "", 0, 0);
				p4 = new Player("4", "", "", 0, 0);
				p5 = new Player("5", "", "", 0, 0);
				p6 = new Player("6", "", "", 0, 0);
//				p3 = new Player();
//				p4 = new Player();
//				p5 = new Player();
//				p6 = new Player();
				
				if(numPlayers==3){
					p3 = records.get(index).getPlayers().get(2);
				}
				if(numPlayers==4){
					p3 = records.get(index).getPlayers().get(2);
					p4 = records.get(index).getPlayers().get(3);
				}
				if(numPlayers==5){
					p3 = records.get(index).getPlayers().get(2);
					p4 = records.get(index).getPlayers().get(3);
					p5 = records.get(index).getPlayers().get(4);
				}
				if(numPlayers==6){
					p3 = records.get(index).getPlayers().get(2);
					p4 = records.get(index).getPlayers().get(3);
					p5 = records.get(index).getPlayers().get(4);
					p6 = records.get(index).getPlayers().get(5);
				}
			}
			
		}
		

	//	Update the game page
		/**
	 * Update page.
	 *
	 * @param turn the turn
	 */
	public void updatePage(int turn, int round){
		
		do{
			System.out.println("                   Welcome to Satir Therapy!");

			boardProcessor.setBoard(players);
			
			System.out.println("This is round " + round + ", This turn is for player " + turn + " - " + players.get(turn-1).getName());
			
			System.out.println("Please select one of the options:");
			System.out.println("===============================================");
			System.out.println("1. Roll a dice!");
			System.out.println("2. Save the game!");
			System.out.println("3. Check my magic tool card!");
			System.out.println("4. Analyze the family relationship!");
			System.out.println("5. Generate a report!");
			System.out.println("0. exit");
			System.out.print("\nEnter the menu item : ");
			
			Scanner scanner = new Scanner(System.in);
			String option = scanner.nextLine(); //get the Menu id form user input
			
			if(option.equals("1")){ //display
				// simulate a rolling dice
//				DiceProcessor dice = new DiceProcessor();
//				int step = dice.ran();
//				System.out.println("Rolling a dice...");
//	//			 hold 2 seconds for rolling a dice
//				halt(2000);
				
//				if(turn==1){
//					System.out.println("Player 1 - " + p1.getName() + " will move " + step + " grid(s)");
//					p1.move(step);
//					triggerGridIncident(p1);
////					break;
//				}
//				else if(turn==2){
//					System.out.println("Player 2 - " + p2.getName() + " will move " + step + " grid(s)");
//					p2.move(step);
//					triggerGridIncident(p2);
////					break;
//				}
//				else if(turn==3){
//					System.out.println("Player 3 - " + p3.getName() + " will move " + step + " grid(s)");
//					p3.move(step);
//					triggerGridIncident(p3);
////					break;
//				}
//				else if(turn==4){
//					System.out.println("Player 4 - " + p4.getName() + " will move " + step + " grid(s)");
//					p4.move(step);
//					triggerGridIncident(p4);
////					break;
//				}
//				else if(turn==5){
//					System.out.println("Player 5 - " + p5.getName() + " will move " + step + " grid(s)");
//					p5.move(step);
//					triggerGridIncident(p5);
////					break;
//				}
//				else{
//					System.out.println("Player 6 - " + p6.getName() + " will move " + step + " grid(s)");
//					p6.move(step);
//					triggerGridIncident(p6);
////					break;
//				}
				
				// hold 3 seconds before screen refresh
//				try {
//					Thread.sleep(3000);
//			    } catch (InterruptedException e) {
//			        e.printStackTrace();
//			    }
			    //*****remark****
				// the following code is used to test whether the tool card grid, event card grid or say & action card grid trigger will trigger event 
				System.out.print("Enter the number of steps for player " + turn + " " + players.get(turn-1).getName() + ": ");
				String nSteps = scanner.next();
				
				if(isInteger(nSteps)){
					if(Integer.parseInt(nSteps) < 0){
						System.out.println("The number of Steps must be positive, not negative");
					}
					else{
						int step = Integer.parseInt(nSteps);
						if(turn==1){
							System.out.println("Player 1 - " + p1.getName() + " will move " + step + " grid(s)");
							p1.move(step);
							triggerGridIncident(p1);
							break;
						}
						else if(turn==2){
							System.out.println("Player 2 - " + p2.getName() + " will move " + step + " grid(s)");
							p2.move(step);
							triggerGridIncident(p2);
							break;
						}
						else if(turn==3){
							System.out.println("Player 3 - " + p3.getName() + " will move " + step + " grid(s)");
							p3.move(step);
							triggerGridIncident(p3);
							break;
						}
						else if(turn==4){
							System.out.println("Player 4 - " + p4.getName() + " will move " + step + " grid(s)");
							p4.move(step);
							triggerGridIncident(p4);
							break;
						}
						else if(turn==5){
							System.out.println("Player 5 - " + p5.getName() + " will move " + step + " grid(s)");
							p5.move(step);
							triggerGridIncident(p5);
							break;
						}
						else{
							System.out.println("Player 6 - " + p6.getName() + " will move " + step + " grid(s)");
							p6.move(step);
							triggerGridIncident(p6);
							break;
						}
					}
				}
				else{
					System.out.println("Please input integer for the number of steps. Try again!");
				}
				// ** end of dummy code **//
				
				
				// check if there is a winner in the game
				if(isWinnerExisted()==true){
					analyzeFamilyRelationship(round);
					System.out.print("Do you want to print a report? ");
					if(displayYN()=="Yes"){
						
					}
				}
			}
			else if(option.equals("2")){ //
				//System.out.print("Save the game");
				saveGame(round);
			}
			else if(option.equals("3")){ //
				checkPlayerMagicTool(turn);
				pauseProgram();
			}
			else if(option.equals("4")){ //
				analyzeFamilyRelationship(round);
			}
			else if(option.equals("5")){ // 
				generateReport(round);
			}
			else if(option.equals("0")){ 
				//call exit function , 
				System.exit(0);
	
			}
			else {
				System.out.println("You should input 0, 1, 2, 3, 4 or 5. Try again!");
			}
		
		}while(true);
	}
	
	// 
	/**
	 * Checks if is integer.
	 *
	 * @param input the input
	 * @return true, if is integer
	 */
	public boolean isInteger(String input) {
		Pattern integerPattern = Pattern.compile("\\d+" );
		Matcher matchesInteger = integerPattern.matcher(input);
		return matchesInteger.matches();
	}
	
	/**
	 * Gets the number of player.
	 *
	 * @return the number of player
	 */
	public int getNumberOfPlayer(){
		return numPlayers;
	}
	
	// Display tool card information 
	/**
	 * Display card info.
	 *
	 * @param turn the turn
	 */
	public void displayCardInfo(int turn){
		
		Player player;
		switch(turn){
			case 1: player = p1; break;
			case 2: player = p2; break;
			case 3: player = p3; break;
			case 4: player = p4; break;
			case 5: player = p5; break;
			default: player = p6; break;
		}
		
		if(player.getPosition()==5 || player.getPosition()==7 
				|| player.getPosition()==13 || player.getPosition()==15 || player.getPosition()==23){

			System.out.println(player.getName() + ", get a tool card: " + player.getMagicTool().toString());
		}		
	}
	
	
	// Display Say & Act card Question
	/**
	 * Display sa question.
	 */
	public void displaySAQuestion(Card card){
		System.out.println("You draw a say/action card as follows: ");
		System.out.println(card.getCardInfo());
		System.out.print("Are you willing to perform the say/action, ");
	}
	
	// Display "Yes" Or "No" option for player to choose
	/**
	 * Display yes or no.
	 *
	 * @return the string
	 */
	public String displayYN(){
		
		Scanner scanner = new Scanner(System.in);
		String answer;
		
		do{
			System.out.print("Yes or No? ");
			answer = scanner.nextLine();
			if(answer.toLowerCase().equals("yes")){
			
				return "Yes";
			}
			else if(answer.toLowerCase().equals("no")){
				return "No";
			}
			else{
				System.out.println("Please enter yes or no only. Try again!");
			}
		
		}while(true);
	}
	
	// Display "Player" and determine who to use the magic tool
	/**
	 * Display other player.
	 */
	public void displayOtherPlayer(){
		Scanner scanner = new Scanner(System.in);
		String playerID = "";
		do{
			System.out.println("----------------------------------------------------------");
			for(int i=0; i<players.size(); i++){
				System.out.println(players.get(i).getID()+ ". " + players.get(i).getName());
//				if(i<players.size())
//					System.out.print(", ");
			}
			System.out.println("----------------------------------------------------------");
			System.out.print("Please enter one of the above player ID(s) to accomplish the say/action: ");
			playerID = scanner.nextLine();
			
			if(isInteger(playerID)){
				if(Integer.parseInt(playerID)<1 || Integer.parseInt(playerID)>players.size()){
					System.out.println("The player ID you entered is invalid." +
							" The player ID should be between 1 and " + players.size() + 
							" Please try again!");
				}				
				else{
//					System.out.print("Are you going to appoint the player with ID " + playerID + " to accomplish the say/action? ");
//					String answer = displayYN();
//					if(answer=="Yes"){
					// hold on for 3 seconds
					halt(3000);
					int index = Integer.parseInt(playerID)-1;
				    System.out.print("Did player " + players.get(index).getName() + " with ID " + playerID + " accomplish the task specified in say/action card? ");
				    String answer = displayYN();
				    Player targetPlayer=new Player();
				    int id = Integer.parseInt(playerID);
				    id--;
				    for(int i=0; i<players.size(); i++){
			    		
			    		if(players.get(i).getID().equals(String.valueOf(id))){
			    			targetPlayer = players.get(i);
			    			break;
			    		}
			    	}
				    
				    if(answer=="Yes"){
				    	targetPlayer.addSelfEsteem(1);
				    	System.out.print(targetPlayer.getName() + " with id " + playerID + " get one self-esteem value.");
				    	break;
				    }else{
//					    	player.addSelfEsteem(1);
		    			System.out.print("You and " + targetPlayer.getName() + " with id " + playerID + " get nothing.");
		    			break;
				    }
				}
//					else{
//						
//					}
//				}				
			}
			else{
				System.out.println("The player ID should be integer. Please try again!");
			}			
		}while(true);
	}
	
	// trigger incident when player steps onto special grid like event grid, say/action grid and magic tool grid
	public void triggerGridIncident(Player player){
		
//		CardProcessor cardProcessor = new CardProcessor();
		// if the player step onto an event grid
		if(boardProcessor.isEventGrid(player.getPosition())){
			boardProcessor.setBoard(players);
			System.out.println(player.getName() + " step onto an event grid");
//			int eventNo= cardProcessor.selectEvent();
			EventCard eventCard = (EventCard)cardProcessor.getCard("EventCard", players);
//			player.addCard(eventCard);
			halt(3000);
			
//			displayEventQuestion(eventNo);
			displayEventQuestion(eventCard);
			
			// if the player has magic crown, ask him/her whether he/she wants to use it
			if(player.hasMagicCrown()){
				System.out.print(player.getName() + ", Do you want to become the decision maker? ");
				String decision = displayYN();
				
				if(decision.equals("Yes")){
					// make the player become the decision maker of the event card
					cardProcessor.setEventCardDecisionMakerID(eventCard, player.getID());
					System.out.print(player.getName() + " becomes the decision maker now.");
					
					// delete the magic crown from the player
					player.deleteCard(eventCard.getCardID());
				}
				
				
//				System.out.println("You have the following magic tool card(s): " + player.getMagicTool());
//				for(int i=0, j=1; i<player.getMagicTool().size(); i++, j++){
//					System.out.print(j + ". " + player.getMagicTool().get(i).toString());
//				}
				
//				String option;
//				Scanner scanner = new Scanner(System.in);
//				
//				do{
//					System.out.print("Which magic tool card do you want to use? ");
//					option = scanner.nextLine();
//					
//					if(isInteger(option)){
//						if(Integer.parseInt(option)> player.getMagicTool().size() || Integer.parseInt(option) < 1){
//							System.out.println("Please input valid option number");
//						}
//						else{
//							int index = Integer.parseInt(option)-1;							
//							
//							if(player.getMagicTool().get(index).getCardName().equals("MagicCrown")){
//								// �]�k�ثa	�ΨӱN�ƥ���M�����ܦ��ۤv
//								System.out.println(cardProcessor.getEventCardDecisionMaker(eventCard) + " will be changed into " + player.getName());
//								cardProcessor.setEventCardDecisionMaker(eventCard, player.getName());
//								magicCrownUsed = true;								
//							}
//							else if(player.getMagicTool().get(index).getCardName().equals("MagicRod")){
//								// �]�k�M	�ΨӱN�ۤv���M�w�N�����M���̪��M�w
//								System.out.println(player.getName() + "/'s decision will replace the decision maker's one.");
//								magicRodUsed = true;
//							}
//							else if(player.getMagicTool().get(index).getCardName().equals("MagicShield")){
//								// �]�k�޵P	�ΨӨ��æۤv���δ�֦ۤv���۴L�߭�
//								System.out.println(player.getName() + "/'s self-esteem value will not be deducted");
//								magicShieldUsed = true;
//							}
//							else if(player.getMagicTool().get(index).getCardName().equals("MagicKey")){
//								// �]�k�_��	��M���̧@�M�w��A�ΥH�V�M���̪�F�ۤv���P��A���δ�֦ۤv���۴L�߭�
//								System.out.println(player.getName() + " could express feelings towards " + cardProcessor.getEventCardDecisionMaker(eventCard) 
//										+ ", So " + player.getName() + "/'s self-esteem value will not be deducted whatever decision is made.");
//								magicKeyUsed = true;
//							}
//							else if(player.getMagicTool().get(index).getCardName().equals("MagicMirror")){
//								// �]�k��	�ΨӤϼu�l�����ƥ�M�w�̡A���δ�֦ۤv���۴L�߭�
//								System.out.println(player.getName() + " could rebound the loss to " + cardProcessor.getEventCardDecisionMaker(eventCard));
//								magicMirrorUsed = true;
//							}
//							else{ // MagicGlove
//								do{
//									// �]�k��M	�Ψӫ�w�䤤�@��a�x����ӧ�������
//									System.out.print(player.getName() + " can appoint one of them to complete the mission: ");
//									for(int i=0; i<players.size(); i++){
//										if(!player.getName().equals(players.get(i).getName())){
//											System.out.print(players.get(i).getName());
//										}
//										if(i < players.size()-1){
//											System.out.print(", ");
//										}
//										else{
//											System.out.print(".");
//										}
//									}
//									System.out.print("Please specify one player to complete the mission or press c for cancel: ");
//									option = scanner.nextLine();
//									
//									if(option.equals("c")){
//										break;
//									}
//									else{
//										boolean assignTask = false;
//										for(int i=0; i<players.size(); i++){
//											if(!option.equals(player.getName()) && option.equals(players.get(i).getName())){
//												System.out.println("You assign the task to " + players.get(i).getName());
//												cardProcessor.setEventCardTarget(eventCard, players.get(i).getName());
//												assignTask = true;
//											}
//										}
//										if(assignTask==true){
////											magicGloveUsed = true;
//											break;
//										}
//										else{
//											System.out.println("You have not assign the task to right person. Try again!");
//										}										
//									}
//								}while(true);								
//							}
//							break;
//						}
//					}
//					else{
//						System.out.println("Please input integer for option. Try again!");
//					}
//				}while(true);
				
			}
			// choose event option
			String option = "";
			Scanner scanner = new Scanner(System.in);
						
			do{
				int makerIndex = Integer.parseInt(cardProcessor.getEventCardDecisionMakerID(eventCard))-1;
				Player decisionMaker = players.get(makerIndex);
				
				int targetIndex = Integer.parseInt(cardProcessor.getEventCardTargetID(eventCard)) - 1;
				Player targetPlayer = players.get(targetIndex);
				
				System.out.print(decisionMaker.getName() + ", Which option would you choose? ");
								
				option = scanner.nextLine();
				
				if(eventCard.getCardName().equals("EventCase1")){
					if(option.equalsIgnoreCase("A")){
						
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 2.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
										+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-2);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-2);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-2);
							break;
						}
					}
					else if(option.equalsIgnoreCase("B")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 1.");
					
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
										+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-1);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-1);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-1);
							break;
						}
					}
					else if(option.equalsIgnoreCase("C")){
						System.out.println(cardProcessor.getEventCardTargetID(eventCard) + "/'s self-esteem will be incremented by 2.");
						targetPlayer.addSelfEsteem(2);
						break;
					}
					else if(option.equalsIgnoreCase("D")){
						System.out.println(player.getName() + "/'s self-esteem will be incremented by 1.");
						targetPlayer.addSelfEsteem(1);
						break;
					}
					else{
						System.out.println("Please input one of the following option: A, B, C and D. Try again!");
					}
				}
				else if(eventCard.getCardName().equals("EventCase2")){
					if(option.equalsIgnoreCase("A")){	
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 2.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
										+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-2);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-2);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-2);
							break;
						}
					}
					else if(option.equalsIgnoreCase("B")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be incremented by 2.");
						targetPlayer.addSelfEsteem(2);
						break;
					}
					else if(option.equalsIgnoreCase("C")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 1.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-1);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-1);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-1);
							break;
						}
					}
					else if(option.equalsIgnoreCase("D")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 1.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-1);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-1);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-1);
							break;
						}
				}else{
					System.out.println("Please input one of the following option: A, B, C and D. Try again!");
				}
			}
			else if(eventCard.getCardName().equals("EventCase3")){
					if(option.equalsIgnoreCase("A")){	
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 1.");

						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-1);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-1);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-1);
							break;
						}
					}
					else if(option.equalsIgnoreCase("B")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 2.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-2);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-2);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-2);
							break;
						}
					}
					else if(option.equalsIgnoreCase("C")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be incremented by 1.");
						targetPlayer.addSelfEsteem(1);
						break;
					}
					else if(option.equalsIgnoreCase("D")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be incremented by 2.");
						targetPlayer.addSelfEsteem(2);
						break;
					}
					else{
						System.out.println("Please input one of the following option: A, B, C and D. Try again!");
					}
				}
				else{	// EventCase4
					if(option.equalsIgnoreCase("A")){	
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 3.");

						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-3);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-3);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-3);
							break;
						}
					}
					else if(option.equalsIgnoreCase("B")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be incremented by 1.");
						targetPlayer.addSelfEsteem(1);
						break;
					}
					else if(option.equalsIgnoreCase("C")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 2.");
						
						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-2);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-2);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-2);
							break;
						}
					}
					else if(option.equalsIgnoreCase("D")){
						System.out.println(targetPlayer.getName() + "/'s self-esteem will be deducted by 1.");

						if(isPlayerMagicToolAvailable(targetPlayer)==true){
							if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicRodUsed")){
								System.out.print(targetPlayer.getName() + " can make decision to replace " 
									+ decisionMaker.getName() + "/'s decision. You can speak your option.");
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicShieldUsed")){
								System.out.println("Since You have used magic shield, your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicKeyUsed")){
								System.out.println("Since You have used magic key, you could express your view so that your self-esteem will not be deducted.");
								break;
							}
							else if(selectPlayerMagicTool(eventCard, targetPlayer).equals("MagicMirrorUsed")){
								System.out.println("Since You have used magic mirror, the loss of self-esteem will be posed on the decision maker.");
								decisionMaker.addSelfEsteem(-1);
								break;
							}
							else{
								// give up use of magic tool
								targetPlayer.addSelfEsteem(-1);
								break;
							}
						}
						else{
							// target player has no magic tool
							targetPlayer.addSelfEsteem(-1);
							break;
						}
					}
					else{
						System.out.println("Please input one of the following option: A, B, C and D. Try again!");
					}
				}
			}while(true);
		}	// if the player step onto a say/action grid
		else if(boardProcessor.isSayAndActionGrid(player.getPosition())){
			boardProcessor.setBoard(players);
			System.out.println(player.getName() + " step onto a say/action grid");
			Card sayAndActionCard= cardProcessor.getCard("SayAndActionCard", players);
//			player.addCard(sayAndActionCard);
			
			displaySAQuestion(sayAndActionCard);
			String answer1 = displayYN();
			
			if(answer1=="Yes"){
				if(player.hasMagicGlove()){
					System.out.print(player.getName() + ", You have magic glove, which allows you to assign the say/action to other member. Do you want to use the magic glove? ");
					String answer2 = displayYN();
					
					if(answer2=="Yes"){
						displayOtherPlayer();
//						answer = displayYN();
//						if(answer=="Yes"){
//							System.out.print("");
//						}
					}
					else{
						player.addSelfEsteem(1);
						System.out.print(player.getName() + " with id " + player.getID() + " get one self-esteem value.");
					}
				}
				else{					
					player.addSelfEsteem(1);
					System.out.println(player.getName() + " with id " + player.getID() + " get one self-esteem value.");					
				}
			}
			else{
				System.out.println(player.getName() + " with id " + player.getID() + " get nothing.");
			}
			
//			halt(3000);
			
		} // if the player step onto a magic tool grid
		else if(boardProcessor.isMagicToolGrid(player.getPosition())){
			boardProcessor.setBoard(players);
			System.out.println(player.getName() + " step onto a magic tool grid");
			Card magicToolCard= cardProcessor.getCard("MagicToolCard", players);
			player.addCard(magicToolCard);
//			displayCardInfo(turn);
			System.out.println(player.getName() + ", you get a magic tool card. " + magicToolCard.getCardInfo());
//			halt(3000);
		}
		else{	// when the player step onto the normal grid
			System.out.println(player.getName() + " step onto a normal grid");			
//			halt(3000);
		}
				
		System.out.println("This turn is ended");
		pauseProgram();
		
	}
		
	// halt the screen for a few seconds
	public void halt(int seconds){
		try {
			System.out.println("Waiting...");
			Thread.sleep(seconds);
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    }
	}
	
	
	public void displayEventQuestion(Card card){
		System.out.println(card.getCardInfo());
	}
	
	public void pauseProgram(){
		System.out.println("Press enter to continue...");
		Scanner keyboard = new Scanner(System.in);
		keyboard.nextLine();
	}
	
	public boolean isWinnerExisted(){
		for(int i=0; i<players.size(); i++){
			if(players.get(i).getSelfEsteem()==10){
				System.out.println("The winner is " + players.get(i).getName());
				return true;
			}
		}
		return false;
	}
	
	public void analyzeFamilyRelationship(int round){
		int totalSelfEsteem=0;
		
		for(int i=0; i<players.size(); i++){
			totalSelfEsteem = totalSelfEsteem + players.get(i).getSelfEsteem();
		}
		System.out.println("The total self-esteem value of your family is " + totalSelfEsteem + ".");
		
		if(totalSelfEsteem/round > 13){
			System.out.println("Your family relationship is very good");
		}
		else if(totalSelfEsteem/round > 11 && totalSelfEsteem/round <= 13){
			System.out.println("Your family relationship is good");
		}
		else if(totalSelfEsteem/round > 9 && totalSelfEsteem/round <= 11){
			System.out.println("Your family relationship is above average");
		}
		else if(totalSelfEsteem/round > 7 && totalSelfEsteem/round <= 9){
			System.out.println("Your family relationship is average");
		}
		else if(totalSelfEsteem/round > 5 && totalSelfEsteem/round <= 7){
			System.out.println("Your family relationship is below average");
		}
		else{
			System.out.println("Your family relationship is poor");
		}
	}
	
	public void checkPlayerMagicTool(int turn){
		int index = turn - 1;
		if(players.get(index).hasMagicTools())
		{
			System.out.println("You have the following magic tool(s): ");
			for(int i=0, j=1; i<players.get(index).getMagicTool().size(); i++, j++){
				System.out.println(j + ". " + players.get(index).getMagicTool().get(i).getCardInfo());
			}
//			System.out.println();
		}
		else{
			System.out.println("You have no magic tool card on hands.");
		}
	}
	
	public void saveGame(int round){
		try {
			Date now = new Date();
			Record record = new Record(now.toString(), numberOfPlayers, players);
			records.add(record);
			
			FileWriter outFile = new FileWriter("file/game.txt");
            PrintWriter out = new PrintWriter(outFile);
            // Write text to file
            // data row: "record date | number of players | player ..."
    		
            for(int i=0; i<records.size(); i++){
            	out.print(records.get(i).getDate() + "|" + records.get(i).getNumberOfPlayers()); 
            	
            	int numPlayers = records.get(i).getPlayers().size();
            	
            	for(int j=0; j<numPlayers; j++){
            		out.print("|" + records.get(i).getPlayers().get(j).getID() 
            				+ "|" + records.get(i).getPlayers().get(j).getName()
            				+ "|" + records.get(i).getPlayers().get(j).getRole()	         
            				+ "|" + records.get(i).getPlayers().get(j).getPosition()
            				+ "|" + records.get(i).getPlayers().get(j).getSelfEsteem()
            				+ "|" + records.get(i).getPlayers().get(j).hasMagicTools());
            		
            		// if the players has magic tool card(s), save the corresponding magic tool cards
            		if(records.get(i).getPlayers().get(j).hasMagicTools()==true){
            			// save the number of magic tool card held by the player
            			out.print("|" + records.get(i).getPlayers().get(j).getMagicTool().size());
            			
            			// save the corresponding magic tool card one by one
            			for(int k=0; k<records.get(i).getPlayers().get(j).getMagicTool().size(); k++)
            			out.print("|" + records.get(i).getPlayers().get(j).getMagicTool().get(k).getCardID()
            					+ "|" + records.get(i).getPlayers().get(j).getMagicTool().get(k).getCardName()
            					+ "|" + records.get(i).getPlayers().get(j).getMagicTool().get(k).getCardInfo()
            					);
            		}
            	}
            	// save the latest card number such that the card number will be issued based on it when the game is loaded
        		out.print("|" + cardProcessor.getCardNo());
        		out.print("|" + round);
            	out.println();
            }          
            out.close();
         } 
		catch (IOException e){
			e.printStackTrace();
        }
	}
	
	public void generateReport(int round){
		
		try {
			FileWriter outFile = new FileWriter("file/report.txt");
            PrintWriter out = new PrintWriter(outFile);
            // Write text to file
            // data row: ""
    		
            for(int i=0, j=1; i<players.size(); i++, j++){
            	out.println(players.get(i).getID() + ", " 
            			+ players.get(i).getName() + " is "
            			+ players.get(i).getRole() + " in the family, He/She got "
            			+ players.get(i).getSelfEsteem() + " value in the game."
            			); 
            	
//            	int numPlayers = records.get(i).getPlayers().size();
//            	
//            	for(int j=0; j<numPlayers; j++){
//            		out.print("|" + records.get(i).getPlayers().get(j).getID() 
//            				+ "|" + records.get(i).getPlayers().get(j).getName()
//            				+ "|" + records.get(i).getPlayers().get(j).getRole()	         
//            				+ "|" + records.get(i).getPlayers().get(j).getPosition()
//            				+ "|" + records.get(i).getPlayers().get(j).getSelfEsteem());
//            	}
//            	out.println();
            }
            int totalSelfEsteem=0;
    		
    		for(int i=0; i<players.size(); i++){
    			totalSelfEsteem = totalSelfEsteem + players.get(i).getSelfEsteem();
    		}
    		out.println("The total self-esteem value of your family is " + totalSelfEsteem + ".");
    		
    		if(totalSelfEsteem/round > 13){
    			out.println("Your family relationship is very good");
    		}
    		else if(totalSelfEsteem/round > 11 && totalSelfEsteem/round <= 13){
    			out.println("Your family relationship is good");
    		}
    		else if(totalSelfEsteem/round > 9 && totalSelfEsteem/round <= 11){
    			out.println("Your family relationship is above average");
    		}
    		else if(totalSelfEsteem/round > 7 && totalSelfEsteem/round <= 9){
    			out.println("Your family relationship is average");
    		}
    		else if(totalSelfEsteem/round > 5 && totalSelfEsteem/round <= 7){
    			out.println("Your family relationship is below average");
    		}
    		else{
    			out.println("Your family relationship is poor");
    		}
            
            out.close();
         } 
		catch (IOException e){
			e.printStackTrace();
        }
		
		/**Location of a file to print**/        
		String fileName = "file/report.txt";         
		/**Read the text content from this location **/        
		String mText = PrintReport.readContentFromFileToPrint(fileName);         
		/**Create an AttributedString object from the text read*/        
		PrintReport.myStyledText = new AttributedString(mText);         
		PrintReport.printToPrinter();     
		
	}
	
	public boolean loadGame(){
		// fill up the vector with player objects and its corresponding magic tool card(s)
							
		try {
			FileInputStream fis = new FileInputStream(new File("file/game.txt"));
	    	BufferedReader input = new BufferedReader(new InputStreamReader(fis));
	    	
	    	String line;
		    StringTokenizer tokens;
		    String date;
		    String numberOfPlayers;
		      
	        while ((line = input.readLine()) != null) {
	            
	            tokens = new StringTokenizer(line, "|");
	            
	            date = tokens.nextToken();
	            numberOfPlayers = tokens.nextToken();
	            int count = Integer.parseInt(numberOfPlayers);
	            
	            for(int i=0; i<count; i++){
	            	String id = tokens.nextToken();
	            	String playerName = tokens.nextToken();
	            	String playerRole = tokens.nextToken();
	            	String position = tokens.nextToken();
	            	String selfEsteemValue = tokens.nextToken();
	            	String hasMagicTools = tokens.nextToken();
	            	
	            	if(hasMagicTools.equals("false")){
	            		players.add(new Player(id, playerName, playerRole, Integer.parseInt(position), Integer.parseInt(selfEsteemValue)));
	            	}
	            	else{
	            		// if the players has magic tool card(s), load the corresponding magic tool cards
	            		String numberOfMagicToolsHeld = tokens.nextToken();
	            		int numMagicToolsHeld = Integer.parseInt(numberOfMagicToolsHeld);
	            		
	            		Vector<Card> cards = new Vector<Card>();
	            		// save the corresponding magic tool card one by one
            			for(int j=0; j<numMagicToolsHeld; j++){
            				String cardID = tokens.nextToken();
            				String cardName = tokens.nextToken();
            				String cardContent = tokens.nextToken();
            				
            				MagicToolCard magicToolCard = new MagicToolCard(Integer.parseInt(cardID), cardName, cardContent);
            				cards.add(magicToolCard);
            			}
            			
            			players.add(new Player(id, playerName, playerRole, Integer.parseInt(position), Integer.parseInt(selfEsteemValue), cards));
	            	}
	            	
	            	
	            }
	            String latestCardNo = tokens.nextToken();
            	cardProcessor.setCardNo(Integer.parseInt(latestCardNo));
            	
            	roundNo = tokens.nextToken();
            	
	            records.add(new Record(date, numberOfPlayers, players));
	        }
	            
	        input.close();
		} 
		catch(Exception ex) {
			ex.printStackTrace();	
		}
		
		if(records.size() > 0){
			return true;
		}					
		else{
			System.out.println("You have no record to load! Please start a new game!");
			return false;
		}
	}
	
	public boolean isPlayerMagicToolAvailable(Player targetPlayer){
		if(targetPlayer.hasMagicShield() || targetPlayer.hasMagicRod() || targetPlayer.hasMagicKey() || targetPlayer.hasMagicMirror()){
			return true;
		}
		return false;
	}
	
	
	public String selectPlayerMagicTool(EventCard eventCard, Player targetPlayer){
		System.out.println(targetPlayer.getName() + ", You have the following magic tool(s) that can be used.");
		for(int i=0; i<targetPlayer.getMagicTool().size(); i++){
			if(targetPlayer.getMagicTool().get(i).getCardName().equals("MagicRod") || 
					targetPlayer.getMagicTool().get(i).getCardName().equals("MagicShield") ||
					targetPlayer.getMagicTool().get(i).getCardName().equals("MagicKey") ||
					targetPlayer.getMagicTool().get(i).getCardName().equals("MagicMirror"))
			System.out.println(targetPlayer.getMagicTool().get(i).getCardInfo());
		}
		
		String option;
		Scanner scanner = new Scanner(System.in);
		boolean IDMatch = false;
		int index = 0;
		
		do{
			System.out.print("Select card ID for which magic tool card that you want to use or press 'c' for cancel: ");
			option = scanner.nextLine();
			
			if(option.equalsIgnoreCase("c")){
				return "None";
			}
						
			if(isInteger(option)){
//				int id = Integer.parseInt(option);
				for(int i=0; i<targetPlayer.getMagicTool().size(); i++){
					if(targetPlayer.getMagicTool().get(i).getCardID()==Integer.parseInt(option)){
						index = i;
						IDMatch=true;
						break;
					}					
				}
				if(IDMatch==true){
					break;
				}
				else{
					System.out.println("Please input valid option number");
				}
			}
			else{
				System.out.println("Please input integer for option. Try again!");
			}
		}while(true);

//		int index = Integer.parseInt(option);
					
		if(targetPlayer.getMagicTool().get(index).getCardName().equals("MagicRod")){
			// 魔法仗	用來將自己的決定代替成決策者的決定
			System.out.println(targetPlayer.getName() + "/'s decision will replace the decision maker's one.");
			return "MagicRodUsed";
		}
		else if(targetPlayer.getMagicTool().get(index).getCardName().equals("MagicShield")){
			// 魔法盾牌	用來防衛自己不用減少自己的自尊心值
			System.out.println(targetPlayer.getName() + "/'s self-esteem value will not be deducted");
			return "MagicShieldUsed";						
		}
		else if(targetPlayer.getMagicTool().get(index).getCardName().equals("MagicKey")){
			// 魔法鑰匙	當決策者作決定後，用以向決策者表達自己的感受，不用減少自己的自尊心值
			System.out.println(targetPlayer.getName() + " could express feelings towards " + cardProcessor.getEventCardDecisionMakerID(eventCard) 
					+ ", So " + targetPlayer.getName() + "/'s self-esteem value will not be deducted whatever decision is made.");
			return "MagicKeyUsed";
		}
		else{ //if(targetPlayer.getMagicTool().get(index).getCardName().equals("MagicMirror")){
			// 魔法鏡	用來反彈損失給事件決定者，不用減少自己的自尊心值
			System.out.println(targetPlayer.getName() + " could rebound the loss to " + cardProcessor.getEventCardDecisionMakerID(eventCard));
			return "MagicMirrorUsed";
		}
	}

	public String getLoadedRoundNo(){
		return this.roundNo;
	}
}
//					else{ // MagicGlove
//						do{
//							// 魔法手套	用來指定其中一位家庭成員來完成任務
//							System.out.print(player.getName() + " can appoint one of them to complete the mission: ");
//							for(int i=0; i<players.size(); i++){
//								if(!player.getName().equals(players.get(i).getName())){
//									System.out.print(players.get(i).getName());
//								}
//								if(i < players.size()-1){
//									System.out.print(", ");
//								}
//								else{
//									System.out.print(".");
//								}
//							}
//							System.out.print("Please specify one player to complete the mission or press c for cancel: ");
//							option = scanner.nextLine();
//							
//							if(option.equals("c")){
//								break;
//							}
//							else{
//								boolean assignTask = false;
//								for(int i=0; i<players.size(); i++){
//									if(!option.equals(player.getName()) && option.equals(players.get(i).getName())){
//										System.out.println("You assign the task to " + players.get(i).getName());
//										cardProcessor.setEventCardTarget(eventCard, players.get(i).getName());
//										assignTask = true;
//									}
//								}
//								if(assignTask==true){
//									magicGloveUsed = true;
//									break;
//								}
//								else{
//									System.out.println("You have not assign the task to right person. Try again!");
//								}										
//							}
//						}while(true);								
//					}
//					break;
//				}
//			}
//			else{
//				System.out.println("Please input integer for option. Try again!");
//			}
//		}while(true);
//	}
//}
